﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using System.Diagnostics;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace DSLFactory.VSCTDesigner
{
    [RuleOn(typeof(VSCTDesignerDiagram), FireTime = TimeToFire.TopLevelCommit, Priority = 0x100000)]
    class ScheduleLayoutRule : ChangeRule
    {
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            if( e.DomainProperty.Id == VSCTDesignerDiagram.TriggerDiagramLayoutDomainPropertyId && (bool)e.NewValue )
            {
                VSCTDesignerDiagram diagram = (VSCTDesignerDiagram)e.ModelElement;
                
                if( (bool)e.OldValue && (bool)e.NewValue) // Layout en cours
                    return;

                diagram.PerformLayout();
                diagram.TriggerDiagramLayout = false;
                diagram.Invalidate(true);
            }
        }
    }


    [RuleOn(typeof(ParentLink), FireTime = TimeToFire.TopLevelCommit, Priority = 0x100000)]
    class ParentLinkLayoutRule : AddRule 
    {
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            ParentLink link = e.ModelElement as ParentLink;
            if( ( link != null ) && ( link.Diagram != null ) )
            {
                VSCTDesignerDiagram diagram = link.Diagram as VSCTDesignerDiagram;
                if( diagram != null && !diagram.TriggerDiagramLayout )
                {
                    diagram.TriggerDiagramLayout = true;
                }
            }
        }
    }

    [RuleOn(typeof(ExternCommandShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(ButtonShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(GroupShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(MenuShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    class CommandShapeLayoutDeleteRule : DeleteRule
    {
        public override void ElementDeleted(ElementDeletedEventArgs e)
        {
            NodeShape modelElement = e.ModelElement as NodeShape;
            if( ( modelElement != null ) && ( modelElement.Diagram != null ) )
            {
                VSCTDesignerDiagram diagram = modelElement.Diagram as VSCTDesignerDiagram;
                if( diagram != null && !diagram.TriggerDiagramLayout )
                {
                    diagram.TriggerDiagramLayout = true;
                }
            }
        }
    }

    [RuleOn(typeof(ExternCommandShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(ButtonShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(GroupShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    [RuleOn(typeof(MenuShape), FireTime = TimeToFire.LocalCommit, Priority = 0x100000)]
    class CommandShapeLayoutRule : ChangeRule
    {
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            if( e.DomainProperty.Id == NodeShape.AbsoluteBoundsDomainPropertyId )
            {
                NodeShape modelElement = e.ModelElement as NodeShape;
                if( ( modelElement != null ) && ( modelElement.Diagram != null ) )
                {
                    VSCTDesignerDiagram diagram = modelElement.Diagram as VSCTDesignerDiagram;
                    if( diagram != null && !diagram.TriggerDiagramLayout)
                    {
                        diagram.TriggerDiagramLayout = true;
                    }
                }
            }
        }
    }
}
